//
//  DotSpreadTransition.m
//  YXPresentOneTransition(转场)
//
//  Created by Rookie_YX on 16/7/12.
//  Copyright © 2016年 YX_Rookie. All rights reserved.
//

#import "DotSpreadTransition.h"
#import "DotSpreadFirstViewController.h"
#import "DotSpreadSecondViewController.h"

@implementation DotSpreadTransition

+ (instancetype)transitionDotSpreadWithTransitionType:(YXDotSpreadTransitionType)type{
  
  return [[self alloc] initDotSpreadWithTransitionType:type];
}

- (instancetype)initDotSpreadWithTransitionType:(YXDotSpreadTransitionType)type{
  
  if (self = [super init]) {
    _type = type;
  }
  return self;
}

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{
  return 0.5;
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
  switch (_type) {
    case YXDotSpreadTransitionTypePresent:
      [self presentAnimation:transitionContext];
      break;
      
    case YXDotSpreadTransitionTypeDismiss:
      [self dismissAnimation:transitionContext];
      break;
  }
}

- (void)presentAnimation:(id<UIViewControllerContextTransitioning>)transitionContext{
  
  UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
  UINavigationController *fromVC = (UINavigationController *)[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
  DotSpreadFirstViewController *temp = fromVC.viewControllers.lastObject;
  UIView *containerView = [transitionContext containerView];
  [containerView addSubview:toVC.view];
  //画两个圆路径
  UIBezierPath * startCycle =  [UIBezierPath bezierPathWithOvalInRect:temp.buttonFrame];
  CGFloat x = MAX(temp.buttonFrame.origin.x, containerView.frame.size.width - temp.buttonFrame.origin.x);
  CGFloat y = MAX(temp.buttonFrame.origin.y, containerView.frame.size.height - temp.buttonFrame.origin.y);
  CGFloat radius = sqrtf(pow(x, 2) + pow(y, 2));
  UIBezierPath * endCycle = [UIBezierPath bezierPathWithArcCenter:containerView.center radius:radius startAngle:0 endAngle:M_PI * 2 clockwise:YES];
  //创建CAShapeLayer进行遮盖
  CAShapeLayer *maskLayer = [CAShapeLayer layer];
  maskLayer.path = endCycle.CGPath;
  //将maskLayer作为toVC.View的遮盖
  toVC.view.layer.mask = maskLayer;
  //创建路径动画
  CABasicAnimation *maskLayerAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
  maskLayerAnimation.delegate = self;
  //动画是加到layer上的，所以必须为CGPath，再将CGPath桥接为OC对象
  maskLayerAnimation.fromValue = (__bridge id)(startCycle.CGPath);
  maskLayerAnimation.toValue = (__bridge id)((endCycle.CGPath));
  maskLayerAnimation.duration = [self transitionDuration:transitionContext];
  maskLayerAnimation.delegate = self;
  maskLayerAnimation.timingFunction = [CAMediaTimingFunction  functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  
  [maskLayerAnimation setValue:transitionContext forKey:@"transitionContext"];
  [maskLayer addAnimation:maskLayerAnimation forKey:@"path"];

}

- (void)dismissAnimation:(id<UIViewControllerContextTransitioning>)transitionContext{
  
  UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
  UINavigationController *toVC = (UINavigationController *)[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
  DotSpreadFirstViewController *temp = toVC.viewControllers.lastObject;
  UIView *containerView = [transitionContext containerView];
  //画两个圆路径
  CGFloat radius = sqrtf(containerView.frame.size.height * containerView.frame.size.height + containerView.frame.size.width * containerView.frame.size.width) / 2;
  UIBezierPath *startCycle = [UIBezierPath bezierPathWithArcCenter:containerView.center radius:radius startAngle:0 endAngle:M_PI * 2 clockwise:YES];
  UIBezierPath *endCycle =  [UIBezierPath bezierPathWithOvalInRect:temp.buttonFrame];
  //创建CAShapeLayer进行遮盖
  CAShapeLayer *maskLayer = [CAShapeLayer layer];
  maskLayer.fillColor = [UIColor greenColor].CGColor;
  maskLayer.path = endCycle.CGPath;
  fromVC.view.layer.mask = maskLayer;
  //创建路径动画
  CABasicAnimation *maskLayerAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
  maskLayerAnimation.delegate = self;
  maskLayerAnimation.fromValue = (__bridge id)(startCycle.CGPath);
  maskLayerAnimation.toValue = (__bridge id)((endCycle.CGPath));
  maskLayerAnimation.duration = [self transitionDuration:transitionContext];
  maskLayerAnimation.delegate = self;
  maskLayerAnimation.timingFunction = [CAMediaTimingFunction  functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  [maskLayerAnimation setValue:transitionContext forKey:@"transitionContext"];
  [maskLayer addAnimation:maskLayerAnimation forKey:@"path"];

}

// 处理到动画的完成逻辑
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
  switch (_type) {
    case YXDotSpreadTransitionTypePresent:{
      id<UIViewControllerContextTransitioning> transitionContext = [anim valueForKey:@"transitionContext"];
      [transitionContext completeTransition:YES];
    }
      break;
    case YXDotSpreadTransitionTypeDismiss:{
      id<UIViewControllerContextTransitioning> transitionContext = [anim valueForKey:@"transitionContext"];
      [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
      if ([transitionContext transitionWasCancelled]) {
        [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey].view.layer.mask = nil;
      }
    }
      break;
  }
}

@end
